/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import utility.ConnectDB;
import model.NhanVien;
import view.frmNhanVien;

/**
 *
 * @author Dom
 */
public class NhanVienController {
    public static void showTable(JTable parent) throws SQLException
    {
        String sql = "SELECT * FROM NhanVien";
        ResultSet bang = ConnectDB.getResultSet(sql);
        
        ResultSetMetaData rsmd = bang.getMetaData();
        
        int column = rsmd.getColumnCount(); //Số cột
        
        DefaultTableModel model = new DefaultTableModel();  //Model đệm trước khi đẩy vào JTable
        
        //Lấy tiêu đề các cột và gán vào model
        String cname[] = new String[column];
        for (int i=0;i<column;i++)
            cname[i]=rsmd.getColumnName(i+1);
        model.setColumnIdentifiers(cname);
        
        
        //Lấy dữ liệu
        while (bang.next())
        {
            String record[] = new String[column];
            for (int i = 0; i<column;i++)
                record[i]=bang.getString(i+1);
            
            model.addRow(record);
            
        }
        //Đẩy model lên JTable
        parent.setModel(model);
    }
    
     public static DefaultComboBoxModel LoaiNV() throws SQLException
     {
         DefaultComboBoxModel temp = new DefaultComboBoxModel();
         String sql="SELECT LoaiNV FROM NhanVien";
         ResultSet bang = ConnectDB.getResultSet(sql);
         while(bang.next())
         {
             temp.addElement(bang.getString(1));
         }
         return temp;
     }
    
    public static void Them(NhanVien nv)
    {
         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "INSERT INTO NHANVIEN(LoaiNV,HoTenNV,NgSinhNV,DiaChiNV,CMND,DienThoaiNV,GioiTinh,NgVao,LuongCB) VALUES (";
                //sql = sql + "'" + nv.getMaNV() + "'";
                sql = sql + "'" + nv.getLoaiNV() + "',";
                sql = sql + "'" + nv.getHoTen() + "',";
                sql = sql + "'" + dateFormat.format(nv.getNgaySinh())+"',";
                sql = sql + "'" + nv.getDiachi() + "',";
                sql = sql + "'" + nv.getCMND() + "',";
                sql = sql + "'" + nv.getSDT() + "',";
                sql = sql + "'" + nv.getGioiTinh() + "',";
                sql = sql + "'" + dateFormat.format(nv.getNgayVaoLam()) + "',";
                sql = sql + "'" + nv.getLuongCB() + "'";
                sql += ")";
         //System.out.println(sql);
                ConnectDB.getResultSet(sql);
         
         //return sql;
         
    }
    
    public static void Delete(int ID)
    {
        String sql = "DELETE FROM NHANVIEN WHERE ID = "+ID;
        ConnectDB.getResultSet(sql);
        //System.out.println(sql);
    }
    
    public static void Update(NhanVien nv)
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql = "UPDATE NHANVIEN SET HoTenNV = ";
            //sql = sql + "'" + nv.getLoaiNV() + "'";
           //sql = sql + ",HoTenNV = ";
            sql = sql + "'" + nv.getHoTen() + "'";
            sql = sql + ",NgSinhNV = ";
            sql = sql + "'" + dateFormat.format(nv.getNgaySinh()) + "'";
            sql = sql + ",DiaChiNV = ";
            sql = sql + "'" + nv.getDiachi() + "'";
            sql = sql + ",CMND = ";
            sql = sql + "'" + nv.getCMND() + "'";
            sql = sql + ",DienThoaiNV = ";
            sql = sql + "'" + nv.getSDT() + "'";
            sql = sql + ",GioiTinh = ";
            sql = sql + "'" + nv.getGioiTinh() + "'";
            sql = sql + ",NgVao = ";
            sql = sql + "'" + dateFormat.format(nv.getNgayVaoLam()) + "'";
            sql = sql + ",LuongCB = ";
            sql = sql + "'" + nv.getLuongCB() + "'";
            sql += " WHERE ID=";
            sql += nv.getMaNV();
            
            ConnectDB.getResultSet(sql);
           // System.out.println(sql);
    }
    
    
}


 
  